package logic

import (
	"context"
	"errors"
	"house_srv/cache"
	"house_srv/mysql"
	"house_srv/pkg"

	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *house.UserLoginRequest) (*house.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	user.Mobile = in.Mobile
	err := user.UserGetMobile(in.Mobile)
	if err != nil {
		logx.Info("手机号未注册,请重新注册", err)
		return nil, errors.New("手机号未注册,请重新注册")
	}
	//if user.Mobile != in.Mobile {
	//	logx.Info("手机号未注册,请重新注册", err)
	//	return nil, errors.New("手机号未注册,请重新注册")
	//}

	get := cache.Rdb.Get(l.ctx, "sendSmslogin"+in.Mobile)
	if get.Val() != in.SendSms {
		logx.Info("获取短信验证码信息失败")
		return nil, errors.New("获取短信验证码信息失败")
	}

	if user.Password != pkg.Md5Encryption(in.Password) {
		logx.Info("密码不一致", err)
		return nil, errors.New("密码不一致")
	}

	return &house.UserLoginResponse{
		UserId: int64(user.ID),
	}, nil
}
